namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorBollingerBands : BaseIndicator
    {
        private LineShape tlsD;
        private LineShape tlsM;
        private LineShape tlsU;

        public IndicatorBollingerBands()
        {
            base.IndType = IndicatorType.BOLL;
            base.IndName = "BOLL";
            base.IndDesc = "布林线";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 20.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "P", 2.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "M", "U", "D" };
        }

        public override bool Contains(object obj)
        {
            if (((obj != this.tlsM) && (obj != this.tlsU)) && (obj != this.tlsD))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curValue = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double agvValue = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.TlsM.Name, base.SourceDic["Close"], curValue, base.Crab.DataSource);
            double num3 = Common.CalculateStandardDeviation(base.SourceDic["Close"], (int) base.IndPeriod[0].ParamValue, base.IndPeriod[1].ParamValue, curValue, agvValue, base.Crab.DataSource, r);
            double num4 = agvValue + num3;
            double num5 = agvValue - num3;
            return new double[] { agvValue, num4, num5 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.TlsM, this.TlsU, this.TlsD };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsD);
            base.Crab.DeleteShape(this.TlsM);
            base.Crab.DeleteShape(this.TlsU);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsM.Name, numArray[0], r);
                        base.Crab.SetValue(this.TlsU.Name, numArray[1], r);
                        base.Crab.SetValue(this.TlsD.Name, numArray[2], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsM = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsU = base.Crab.AddLine(fields[1], panelID);
            base.Crab.SetLine(fields[1], fields[1], Color.Yellow, 1, 0.01, 2, DashStyle.Solid);
            this.TlsD = base.Crab.AddLine(fields[2], panelID);
            base.Crab.SetLine(fields[2], fields[2], Color.FromArgb(0xff, 0, 0xff), 1, 0.01, 2, DashStyle.Solid);
            base.SourceDic["Close"] = source[0];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
        }

        public LineShape TlsD
        {
            get
            {
                return this.tlsD;
            }
            set
            {
                this.tlsD = value;
            }
        }

        public LineShape TlsM
        {
            get
            {
                return this.tlsM;
            }
            set
            {
                this.tlsM = value;
            }
        }

        public LineShape TlsU
        {
            get
            {
                return this.tlsU;
            }
            set
            {
                this.tlsU = value;
            }
        }
    }
}

